<template>
  <div class="main-app">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="600px"
      style="margin-top: 50px"
    >
      <el-form-item label="废物类别:" style="width: 60%">
        <el-input v-model="form.number" disabled />
      </el-form-item>
      <el-form-item label="废物类别编码:" prop="code" style="width: 60%">
        <el-input
          v-model="form.code"
          clearable
          :disabled="isView"
          @change="changeCode"
        />
      </el-form-item>
      <el-form-item label="废物类别标号:" prop="grade" style="width: 60%">
        <el-input
          v-model="form.grade"
          clearable
          :disabled="isView"
          @change="changeGrade"
        />
      </el-form-item>
      <el-form-item label="状态:" prop="status">
        <el-radio-group v-model="form.status" :disabled="isView">
          <el-radio
            v-for="(item, index) in options"
            :key="index"
            :label="item.value"
          >{{ item.label }}</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <el-row style="margin-top: 40px; text-align: center">
      <el-form :inline="true" class="form-inline">
        <el-form-item>
          <el-button type="primary" :disabled="isView" @click="save">提交</el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="cancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-row>
  </div>
</template>

<script>
import { EquipmentStatus } from "../../../utils/resource";
import { save, getById } from "../../../api/system";
export default {
  data() {
    return {
      form: {
        code: "",
        status: "0",
        number: "",
        grade: "",
      },
      options: EquipmentStatus,
      rules: {
        code: [
          { required: true, message: "请输入废物类别编码", trigger: "blur" },
        ],
        grade: [
          { required: true, message: "请输入废物类别标号", trigger: "blur" },
        ],
        status: [{ required: true, message: "请选择状态", trigger: "change" }],
      },
      id: "",
    };
  },
  computed: {
    isView() {
      // return this.$route.name === "type-view";
      return this.$route.name === "废物类别详情";
    },
  },
  async created() {
    const { id, type } = this.$route.params;
   		 await this.getById(id, type);
   		 this.id = id;
  },
  methods: {
    changeCode() {
      this.form.number = `${this.form.code}${this.form.grade}`;
    },
    changeGrade() {
      this.form.number = `${this.form.code}${this.form.grade}`;
    },

	   async getById(id, type) {
	     const { data } = await getById({ type: type, code: id });
      this.form.number = data.customThree;
      this.form.code = data.customOne;
      this.form.grade = data.customTwo;
      this.form.status = data.status.toString();
    },
    save() {
      console.log(this.id);
      this.$refs["form"].validate(async(valid) => {
        if (valid) {
          let data = {
            type: "103",
            code: this.id,
            customOne: this.form.code,
            status: this.form.status,
            value: this.form.number,
            customTwo: this.form.grade,
            customThree: this.form.number,
          };
          console.log(data);

          let message = "";

          if (!this.id) {
			   data = Object.assign({ isAdd: "1" }, data);
            message = "废物类别新增成功";
          } else {
            message = "废物类别修改成功";
          }
          const { success, msg } = await save(data);
          if (success) {
            this.$notify({
              title: "成功",
              message: message,
              type: "success",
              duration: 3000,
            });
            this.$router.push("/system/type");
          } else {
            this.$notify.error({
              title: "失败",
              message: msg,
              duration: 3000,
            });
          }
        }
      });
    },
    cancel() {
      this.$router.push("/system/type");
    },
  },
};
</script>
